package com.pww.movie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pww.movie.domain.PO.User;
import com.pww.movie.domain.VO.UserVO;
import com.pww.movie.mapper.UserMapper;
import com.pww.movie.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pww.movie.utils.Md5Util;
import com.pww.movie.utils.Result;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author pww
 * @since 2025-05-22
 */
@Service
@AllArgsConstructor
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private UserMapper userMapper;

    @Override
    public Result<UserVO> getByUserName(String username) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        User user = userMapper.selectOne(wrapper);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return Result.success(userVO);
    }


    /**
     * 注册
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @Override
    public Result register(String username, String password) {
        //对密码进行md5加密
        String md5String = Md5Util.getMD5String(password);
        log.info("加密后的密码:{}", md5String);
        Boolean flag = userMapper.addUser(username, md5String);
        if (flag) {
            return Result.success();
        }
        return Result.error("注册失败！");
    }
}
